package com.common.base.app.extras

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.common.base.app.activity.BaseActivity
import com.common.base.app.fragment.BaseFragment
import com.common.base.utils.ToastUtils
import com.common.base.utils.ViewUtils
import com.common.base.widget.PlaceholderView

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/8/19
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
fun BaseActivity.getView(): View? {
    return this.window.decorView
}

fun <T : View> BaseActivity.findView(id: Int): T? {
    if (getView() == null) throw NullPointerException("ViewDelegate：rootView not be null.")
    return getView()?.findViewById(id)
}

fun BaseActivity.setVisibility(id: Int, visibility: Int) {
    setVisibility(findView<View>(id), visibility)
}

fun BaseActivity.setVisibility(view: View?, visibility: Int) {
    view?.visibility = visibility
}

fun BaseActivity.setVisibility(id: Int, isVisible: Boolean) {
    setVisibility(findView<View>(id), isVisible)
}

fun BaseActivity.setVisibility(view: View?, isVisible: Boolean) {
    if (isVisible) {
        setVisible(view)
    } else {
        setGone(view)
    }
}

fun BaseActivity.setViIn(view: View?, isVisible: Boolean) {
    if (isVisible) {
        setVisible(view)
    } else {
        view?.visibility = View.INVISIBLE
    }
}

fun BaseActivity.setVisible(id: Int) {
    setVisible(findView<View>(id))
}

fun BaseActivity.setVisible(view: View?) {
    if (view?.visibility != View.VISIBLE) {
        view?.visibility = View.VISIBLE
    }
}

fun BaseActivity.setGone(id: Int) {
    setGone(findView<View>(id))
}

fun BaseActivity.setGone(view: View?) {
    if (view?.visibility != View.GONE) {
        view?.visibility = View.GONE
    }
}

fun BaseActivity.setText(id: Int, text: CharSequence?) {
    setText(findView<TextView>(id), text)
}

fun BaseActivity.setText(id: Int, resId: Int) {
    setText(id, getString(resId))
}

fun BaseActivity.setText(textView: TextView?, text: CharSequence?) {
    ViewUtils.setText(textView, text)
}

fun BaseActivity.setText(textView: TextView?, resId: Int) {
    setText(textView, getString(resId))
}

fun BaseActivity.setTextColor(textView: TextView?, color: Int) {
    textView?.setTextColor(color)
}

fun BaseActivity.setTextColor(id: Int, color: Int) {
    setTextColor(findView<TextView>(id), color)
}

fun BaseActivity.setTextSize(id: Int, size: Float) {
    setTextSize(findView<TextView>(id), size)
}

fun BaseActivity.setTextSize(textView: TextView?, size: Float) {
    textView?.textSize = size
}

fun BaseActivity.setImageResource(imageView: ImageView?, resId: Int) {
    imageView?.setImageResource(resId)
}

fun BaseActivity.setImageBitmap(imageView: ImageView?, bitmap: Bitmap?) {
    imageView?.setImageBitmap(bitmap)
}

fun BaseActivity.setImageDrawable(imageView: ImageView?, drawable: Drawable?) {
    imageView?.setImageDrawable(drawable)
}

fun BaseActivity.showToast(content: String?) {
    ToastUtils.showToast(content)
}

fun BaseActivity.showToast(resId: Int) {
    ToastUtils.showToast(resId)
}

fun BaseActivity.showCustomToast(content: String?) {
}

fun BaseActivity.showCustomToast(resId: Int) {
}

fun BaseActivity.showLongToast(content: String?) {
    ToastUtils.showLong(content)
}

fun BaseActivity.showLongToast(resId: Int) {
    ToastUtils.showLong(resId)
}

fun BaseActivity.showDialogToast(resId: Int) {
}

fun BaseActivity.showDialogToast(content: String?) {
}

fun BaseActivity.showDialogToast(content: String?, duration: Long) {
}

fun BaseActivity.showPageLoading(placeholderView: PlaceholderView?) {
    placeholderView?.showLoading()
}

fun BaseActivity.showPageEmpty(placeholderView: PlaceholderView?, msg: String?) {
    placeholderView?.showEmpty(msg)
}


fun BaseActivity.showPageError(placeholderView: PlaceholderView?, msg: String?) {
    placeholderView?.showError(msg)
}

fun BaseActivity.setPageErrorRetryListener(
    placeholderView: PlaceholderView?,
    listener: () -> Unit
) {
    placeholderView?.setOnReloadWhenErrorOrEmpty(listener)
}

fun BaseActivity.showPageContent(placeholderView: PlaceholderView?) {
    placeholderView?.showContent()
}